package lu.med.praxiswahl.wahlumed.client.cashbook;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

import lu.med.praxiswahl.wahlumed.client.cashbook.CashBookDataModel;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * 
 * @author Lars Holger Roth
 */
public class CashBookCellRenderer implements TableCellRenderer
{
    private Color black = new Color(0, 0, 190);
    private Color white = new Color(255, 255, 255);
    private Color red = new Color(235, 0, 0);
    private Color green = new Color(46, 139, 87);
    /*
     * private Color grey = new Color(255, 255, 255); private Color darkGrey =
     * new Color(200,200,200); private Color yellow = new Color(200, 200, 0);
     */
    private Font bold = new Font("Arial", Font.BOLD, 12);
    private Color blue = new Color(0, 0, 250);
    private CashBookDataModel model;
    private Main dataManager;
    private String str;

    public CashBookCellRenderer(CashBookDataModel model, Main dataManager)
    {
	this.model = model;
	this.dataManager = dataManager;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
	    boolean isSelected, boolean hasFocus, int row, int column)
    {
	JLabel label = new JLabel();

	label.setOpaque(true);
	Border b = BorderFactory.createEmptyBorder(1, 1, 1, 1);
	label.setBorder(b);
	label.setFont(table.getFont());
	label.setForeground(table.getForeground());
	label.setBackground(table.getBackground());

	// Sonderf�lle f�r die Farben
	// Jedes Datum wird Rot eingefärbt

	if (model == null)
	{
	    label.setText("");
	}
	else
	{
	    if (column == 2)
	    {
		str = (String) model.getValueAt(row, column);
		label.setText(str);
		if (!str.equals("     \""))
		{
		    label.setForeground(black);
		    label.setBackground(white);
		    label.setFont(bold);
		}
	    }
	    else if (column == 5)
	    {
		if(row == 0){
		    label.setText("-/-");
		}else if(row > 0){
		    
		    Double valueTmp = (Double) model.getValueAt(row, column);
			if(valueTmp != 0.0d){
			    label.setText(""+valueTmp);
			}
			else{
			    label.setText("");
			}
		}
		else{
		  label.setText("" + (Double) model.getValueAt(row, column));
		}
		
	    }
	    else if (column == 6)
	    {
		
		if(row > 0){
		    
		    Double valueTmp = (Double) model.getValueAt(row, column);
			if(valueTmp != 0.0d){
			    label.setText(""+valueTmp);
			}
			else{
			    label.setText("");
			}
		}
		else{
		  label.setText("" + (Double) model.getValueAt(row, column));
		}
		
		
		label.setFont(bold);
	    }
	    else if (column == 7)
	    {
		String valueTmp = (String) model.getValueAt(row, column);
		if(Double.parseDouble(valueTmp) != 0.0d){
		    label.setText(valueTmp);
		}
		else{
		    label.setText("");
		}
		
	    }
	    
	    else if (column == 8)
	    {
		
		Double valueTmp = (Double) model.getValueAt(row, column);
		if(valueTmp != 0.0d){
		    label.setText(""+valueTmp);
		}
		else{
		    label.setText("");
		}
	    }
	    else if (column == 9)
	    {
		label.setText("" + (Double) model.getValueAt(row, column));
		label.setForeground(green);
		label.setBackground(white);
		label.setFont(bold);
	    }
	    else
		label.setText((String) model.getValueAt(row, column));
	}

	if (hasFocus)
	{
	    label.setBackground(black);
	    label.setForeground(white);
	}
	else
	{
	    // Angezeigte Spalte in Modellspalte umwandeln
	    column = table.convertColumnIndexToModel(column);
	}

	if (isSelected)
	{
	    label.setBackground(blue);
	    label.setForeground(white);
	    label.setFont(bold);
	}

	return label;
    }
}
